﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySpace.Data;
using System.Xml;
using DataBaseTable;

public partial class GuestBook : System.Web.UI.Page
{
    protected static int nowpages;
    GuestBookSetting gbs = new GuestBookSetting();
    protected void Page_Load(object sender, EventArgs e)
    {
        Master.WebSubTitle += "留言板-";
        if (!IsPostBack)
        {
            nowpages = 1;
            this.bdguestbook();
            
        }
    }
    protected void bdguestbook()
    {
        DBHandler dbh = GetHandler.Init();
        if (gbs.Ireplay == 1)
        {
            dbh.CommandText = "select * from hwguestbook where len(reply) > 0 or IsTop = 1  order by id desc";
        }
        else
        {
            dbh.CommandText = "select * from hwguestbook order by IsTop desc,ID desc";
        }
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = gbs.PageSize;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        curpage.Text = nowpages.ToString();
        sumpage.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        guest_list.DataSource = ps;
        guest_list.DataBind();

    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        this.bdguestbook();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        this.bdguestbook();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        this.bdguestbook();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        this.bdguestbook();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(sumpage.Text);
        this.bdguestbook();
    }
    protected void Submit_btn_Click(object sender, EventArgs e)
    {
        if (!this.check())
            return;
        if (gbs.ForbidIP.Contains(GetClientIP.get()))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('对不起,暂时不能留言!!');</script>");
        }
        GuestBookTable gbt = new GuestBookTable();
        gbt.Name = Name_tb.Text;
        gbt.Email = Email_tb.Text;
        gbt.Content = Content_tb.Text;
        gbt.Ip = GetClientIP.get();

        if (!gbt.CheckTimeSpace(GetClientIP.get(), gbs.VTime))
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('请不要频繁留言，请等待" + gbs.VTime / 60 + "分钟留言!!');</script>");
            return;
        }

        try
        {
            gbt.Insert();
            if (gbs.Ireplay == 1)
            {

                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('留言已提交,留言需要管理员回复后才能显示!!');</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('发布留言成功,感谢你的支持!!');</script>");
            }
            Name_tb.Text=string.Empty;
            Email_tb.Text = string.Empty;
            Content_tb.Text = string.Empty;
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('对不起,留言失败!!');</script>");
        }
    }
    private bool check()
    {
        if (Name_tb.Text.Trim().Equals(""))
            return false;
        if (Name_tb.Text.Length > 20)
            return false;
        if (Email_tb.Text.Trim().Equals(""))
            return false;
        if (Content_tb.Text.Trim().Equals(""))
            return false;
        if (Content_tb.Text.Length > 1000)
            return false;
        return true;
    }
}